<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var queryParams = {};
var tables = $('#dataTables').DataTable({
    "sAjaxSource" : '{{ urlfor "BadreportController.Badreport"}}'
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        queryParams = checkQueryTableData(aoData);
        var form = $('.query-form').serializeArray();
        $.each(form, function(i, value){
            queryParams[value.name] = value.value;
        });
        $.post(sSource, JSON.stringify(queryParams),function(resp) {
            fnCallback(resp);
        },'json')
    }
    ,columns: [
        {render:function(data, type, full, meta) {
            return full.Number;
        }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.BadreportUser;
        }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return timetransDate(full.BadreportTime);
        }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.AuditingUser == '' ? '-' : full.AuditingUser;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.AuditingTime> 0 ? timetransDate(full.AuditingTime) : '-';
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.ExecuteUser != '' ? full.ExecuteUser : '-';
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.ExecuteTime > 0  ? timetransDate(full.ExecuteTime) : '-';
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.TotalGoodsNum;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                switch (full.Auditing) {
                      case 0:
                          return '<span class="am-badge am-badge-warning">待审核</span>';
                          break;
                    case 1:
                        return '<span class="am-badge am-badge-success">通过审核待执行</span>';
                        break;
                    case 2:
                        return '<span class="am-badge">已驳回</span>';
                        break;
                    case 3:
                        return '<span class="am-badge am-badge-success">已执行</span>';
                        break;
                    case 4:
                        return '<span class="am-badge">驳回执行</span>';
                        break;
                }
                return '';
            }, "bSortable": false}
        ,{
            "mDataProp": "Action",
            "bSortable": false,
            "sWidth":"10%",
            "render": function ( data, type, full, meta ) {
				if(full.Auditing == 0) {
					btns = '<a href="{{urlfor "BadreportController.BadreportView"}}/'+ full.Id +'" class="am-btn am-btn-success am-btn-xs">审核</a>';
				}else {
					btns = '<a href="{{urlfor "BadreportController.BadreportView"}}/'+ full.Id +'" class="am-btn am-btn-success am-btn-xs">详情</a>';
				}
                if(full.Auditing == 2 || full.Auditing == 0 || full.Auditing == 4) {
                    btns += '&nbsp;<a href="javascript:;" class="am-btn am-btn-danger am-btn-xs" onclick="delBadreport('+ full.Id +');return false;">删除</a>';
                }
                return btns;
            }
        }
    ]
    ,"aLengthMenu":[20, 50, 100, 200]
	,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
});

$("#query-submit").bind("click", function () { //点击按钮 触发table重新请求服务器
    $("#dataTables").dataTable().fnDraw(false);
    return false;
});

$('#insert-badreport').click(function(){
    var url = '{{urlfor "BadreportController.ChooseTrader"}}';
    layer.open({
        type: 2,
        title: '生成报损单',
        shadeClose: false,
        shade: 0.2,
        maxmin: false,
        shift: 1,
        area: ['410px', '360px'],
        content: url
    });

    return false;
});
//删除
function delBadreport(id) {
    layer.confirm("确定要删除该单据吗？", function (index) {
        layer.close(index);
        $.post('{{ urlfor "BadreportController.BadreportDelete" }}', {'id':id}, function(res){
            if(res.code == 1) {
                layer.msg(res.msg);return false;
            }
            $("#query-submit").click();
        }, 'json');
    });
}

function SelectChoosetraderRollBack(id) {
    layer.closeAll();
    location = '{{ urlfor "BadreportController.BadreportAdd"}}/'+id;
}
</script>
